(function( factory ) {
    if ( typeof define === "function" && define.amd ) {
        define( ["jquery", "../jquery.validate"], factory );
    } else {
        factory( jQuery );
    }
}(function( $ ) {
	$.extend($.validator.messages, {
	    required: "该字段必填",
	    remote: "请修正此字段",
	    email: "请输入有效的电子邮件地址",
	    url: "请输入有效的网址",
	    date: "请输入有效的日期",
	    dateISO: "请输入有效的日期 (YYYY-MM-DD)",
	    number: "请输入有效的数字",
	    digits: "只能输入数字",
	    creditcard: "请输入有效的信用卡号码",
	    equalTo: "你的输入不相同",
	    extension: "请输入有效的后缀",
	    maxlength: $.validator.format("最多可以输入 {0} 个字符"),
	    minlength: $.validator.format("最少要输入 {0} 个字符"),
	    rangelength: $.validator.format("请输入长度在 {0} 到 {1} 之间的字符串"),
	    range: $.validator.format("请输入范围在 {0} 到 {1} 之间的数值"),
	    max: $.validator.format("请输入不大于 {0} 的数值"),
	    min: $.validator.format("请输入不小于 {0} 的数值")
	});
	
	$.validator.addMethod("phoneOrEmail", function(value, element, param) {
		var pattern = /(\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)|(^1[34578]\d{9}$)/;
	    return this.optional(element) || pattern.test(value);   
	}, "邮箱地址或者手机号码格式不正确");
	
	$.validator.addMethod("length", function(value, element, param) {
	    return this.optional(element) || value.length == param;   
	}, $.validator.format("该字符串长度只能为 {0} 个字符"));
	
	$.validator.setDefaults({
		focusInvalid: true,
		focusCleanup: false,
		errorElement: 'div',
		errorPlacement : function(error, element) {
			$(element.parent()).append(error);
		//	$(element).closest('.weui_cell').addClass('weui_cell_warn');
		},
		success: function(error, element){
			if($(element).hasClass('valid') || $(error).html() == ''){
			//	$(element).closest('.weui_cell').removeClass('weui_cell_warn');
			} else {
			//	$(element).closest('.weui_cell').addClass('weui_cell_warn');
			}
		}
	});

	$.extend({
		showResult: function(result){
			if(result.errorFields){
				for(key in result.errorFields){
					$('#'+key+'-error').text(result.errorFields[key]);
				}
			}
		}
	});
}));
